package GUI;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;
import Client_logic.Customer;
import java.awt.SystemColor;

/**
 * This class use to show and update customer details.
 * @author michael
 * @author ido
 * @author matan
 * @author sharon
 */
public class CustomerForm extends JPanel
{
	private JLabel cIdLabel = null;
	private JLabel cNameLabel= null;
	private JLabel cAgeLabel= null;
	private JLabel cAreaLabel= null;
	private JLabel cPhoneLabel= null;
	private JLabel cImportance = null;
	private JTextField cIdTextField = null;
	private JTextField cNameTextField = null;
	private JTextField cAgeTextField = null;
	private JComboBox cAreaJComboBox= null;
	private JTextField cPhoneTextField= null;
	private JTextField cImportanceTextField= null;
	private JButton saveButton = null;
	private JButton cancelButton = null;
	private JButton updateButton = null;
	private JComboBox sectionJComboBox= null;
	private JLabel sectionLabel = null;
	private JLabel allsectionsproductLabel = null;
	private JTextArea sectionsProductTextArea = null;
	private JButton acceptButton = null;
	private JTextArea allAuthorizationTextArea = null;
	private JLabel allAuthorizationLabel = null;
	private Customer customer = null;
	private JScrollPane authorizationpane = null;	
	private JScrollPane sectionScrollPane = null;	
	
	
	/**
	 * This is the default constructor
	 */
	public CustomerForm() 
	{
		super();
		setBackground(SystemColor.activeCaption);
		initialize();	
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() 
	{
		cIdLabel = new JLabel();
		cIdLabel.setBounds(new Rectangle(15, 20, 130, 22));
		cIdLabel.setText("Customer ID:");
		
		cNameLabel = new JLabel();
		cNameLabel.setBounds(new Rectangle(15, 50, 130, 22));
		cNameLabel.setText("Customer name:");
		
		cAgeLabel = new JLabel();
		cAgeLabel.setBounds(new Rectangle(15, 80, 130, 22));
		cAgeLabel.setText("Customer age:");
		
		cAreaLabel = new JLabel();
		cAreaLabel.setBounds(new Rectangle(15, 110, 130, 22));
		cAreaLabel.setText("Customer area:");
		
		cPhoneLabel = new JLabel();
		cPhoneLabel.setBounds(new Rectangle(15,140,130,22));
		cPhoneLabel.setText("Customer phone");
		
		cImportance = new JLabel();
		cImportance.setBounds(new Rectangle(15,170,130,22));
		cImportance.setText("Customer importance");
		
		customer = new Customer(0,"", 0, 0,"");
		
		this.setSize(300, 367);
		this.setLayout(null);
		this.add(cIdLabel, null);
		this.add(cNameLabel, null);
		this.add(cAgeLabel, null);
		this.add(cAreaLabel, null);
		this.add(cPhoneLabel,null);
		this.add(cImportance,null);
		this.add(getCIdTextField(), null);
		this.add(getCNameTextField(), null);
		this.add(getCAageTextField(), null);
		this.add(getCAreaJComboBox(), null);
		this.add(getCPhoneTextField(),null);
		this.add(getCImportanceTextField(),null);
		this.add(getCancelButton(),null);
	}
	
	/**
	 * This method initializes cIdTextField	
	 */
	private JTextField getCIdTextField()
	{
		if (cIdTextField == null) 
		{
			cIdTextField = new JTextField();
			cIdTextField.setBounds(new Rectangle(150, 20, 127, 25));
		}
		return cIdTextField;
	}
	
	/**
	 * This method initializes cNameTextField	
	 */
	private JTextField getCNameTextField()
	{
		if (cNameTextField == null) 
		{
			cNameTextField = new JTextField();
			cNameTextField.setBounds(new Rectangle(150, 50, 127, 25));
		}
		return cNameTextField;
	}
	
	/**
	 * This method initializes cAgeTextField	
	 */
	private JTextField getCAageTextField()
	{
		if (cAgeTextField == null) 
		{
			cAgeTextField = new JTextField();
			cAgeTextField.setBounds(new Rectangle(150, 80, 127, 25));
		}
		return cAgeTextField;
	}
	
	/**
	 * This method initializes cAreaTextField	
	 */
	private JComboBox getCAreaJComboBox()
	{
		if (cAreaJComboBox == null) 
		{
			cAreaJComboBox = new JComboBox();
			cAreaJComboBox.setBounds(new Rectangle(150, 110, 127, 25));
			String[] areaList = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				areaList = (String[]) connectiontoserver.loadAllAreas();
				if (areaList == null)
				{
					JOptionPane.showMessageDialog(null, "ERROR!!!");
					cAreaJComboBox.addItem(":");
				}	
				else
				{
					int len = areaList.length;
					if (len == 0)
						cAreaJComboBox.addItem("0:EMPTY");						
					for(int i=0; i < len; i++)
					{
						cAreaJComboBox.addItem(areaList[i]);
					}	
				}
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				cAreaJComboBox.addItem(":");
			}
			cAreaJComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					
				}	
			});	
		}
		return cAreaJComboBox;
	}
	
	/**
	 * This method initializes cPhoneTextField	
	 */
	private JTextField getCPhoneTextField()
	{
		if (cPhoneTextField == null) 
		{
			cPhoneTextField = new JTextField();
			cPhoneTextField.setBounds(new Rectangle(150, 140, 127, 25));
		}
		return cPhoneTextField;
	}
	
	/**
	 * This method initializes cImportanceTextField	
	 */
	private JTextField getCImportanceTextField()
	{
		if (cImportanceTextField == null) 
		{
			cImportanceTextField = new JTextField();
			cImportanceTextField.setBounds(new Rectangle(150, 170, 127, 25));
			cImportanceTextField.setEditable(false);
		}
		return cImportanceTextField;
	}
	
	/**
	 * This method initializes cancelButton
	 */	
	public JButton getCancelButton()
	{
		if (cancelButton == null)
		{
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(new Rectangle(15, 520, 120, 29));
		}	
		return cancelButton;				
	}
	
	/**
	 * This method initializes SaveButton	
	 */
	private JButton getSaveButton() 
	{
		if (saveButton == null) 
		{
			saveButton = new JButton();
			saveButton.setBounds(new Rectangle(150, 220, 120, 29));
			saveButton.setText("ADD");
			saveButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String stready = updateCustomer();
					if (stready.equals("Ready"))
					{
						try
						{
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							String msgfromserver = (String)connectiontoserver.addCustomer(customer);
							if (msgfromserver != null)
							{
								if (msgfromserver.equals("OK"))
									JOptionPane.showMessageDialog(null, "The customer successfully saved");	
							}
							else
								JOptionPane.showMessageDialog(null, "FATAL ERROR");
						}catch (ClientNotInitializesException excp)
						{
							JOptionPane.showMessageDialog(null, "ERROR");	
						}	
					}					
				}				
			});						
		}
		return saveButton;
	}
	
	/**
	 * This method update customer to data from the form
	 */
	private String  updateCustomer()
	{
		try 
		{
			long id = Long.parseLong(cIdTextField.getText());
			String customername= cNameTextField.getText();
			int age = Integer.parseInt((cAgeTextField.getText()));
			String areastr = (String)cAreaJComboBox.getSelectedItem();
			int  area = Integer.parseInt(areastr.substring(0, areastr.indexOf(":")));
			String phone = cPhoneTextField.getText();
			if (id >0 &&  !customername.equals("") && age > 0 && area >= 0 && !phone.equals(""))
			{
				customer.setCustID(id);
				customer.setCustName(customername);
				customer.setCustAge(age);
				customer.setCustAddressZone(area);
				customer.setCustPhone(phone);
				return "Ready";
			}							
			else
			{
				JOptionPane.showMessageDialog(null, "Requested fields not filled");
				return "ERROR";
			}			
		}
		catch (Exception excp)
		{
			JOptionPane.showMessageDialog(null, "ILEGAL INPUT");
			return "ERROR";
		}	
	}
	
	/**
	 * This method initializes updateButton	
	 */
	private JButton getUpdateButton() 
	{
		if (updateButton == null) 
		{
			updateButton = new JButton();
			updateButton.setBounds(new Rectangle(150, 220, 120, 29));
			updateButton.setText("Update");
			updateButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String stready = updateCustomer();
					if (stready.equals("Ready"))
					{
						try
						{
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							String msgfromserver = (String)connectiontoserver.updateCustomer(customer);
							if (msgfromserver != null)
							{
								if (msgfromserver.equals("OK"))
									JOptionPane.showMessageDialog(null, "The customer successfully update");
								else
									JOptionPane.showMessageDialog(null, "The customer not update!!!","WARNING", JOptionPane.WARNING_MESSAGE);
							}
							else
								JOptionPane.showMessageDialog(null, "ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
						}catch (ClientNotInitializesException excp)
						{
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
						}	
					}		
				}					
			});						
		}
		return updateButton;
	}
	
	/**
	 * This method load customer details to the form 
	 * @param customer
	 */
	public void loadCustomer(Customer customer)
	{
		this.customer = customer;
		cIdTextField.setText(String.valueOf(customer.getCustID()));
		cNameTextField.setText(customer.getCustName()) ;
		cAgeTextField.setText(String.valueOf(customer.getCustAge()));
		
		cAreaJComboBox.setSelectedItem(customer.getCustAddressZone());
		cPhoneTextField.setText(customer.getCustPhone()) ;
		cImportanceTextField.setText(String.valueOf(customer.getImportance()));		
	}
	
	/**
	 * This method initializes sectionJComboBox	
	 */
	private JComboBox getSectionJComboBox()
	{
		if (sectionJComboBox == null) 
		{
			sectionJComboBox = new JComboBox();
			sectionJComboBox.setBounds(new Rectangle(150, 230, 127, 25));
			String[] sectionList = null;
			try
			{
				ConnectionToServer connectiontoserver = new ConnectionToServer();		
				sectionList = (String[]) connectiontoserver.loadAllSection();
				if (sectionList == null)
				{
					JOptionPane.showMessageDialog(null, "ERROR!!!");
					sectionJComboBox.addItem(":");
				}	
				else
				{
					int len = sectionList.length;
					if (len == 0)
						sectionJComboBox.addItem("0:EMPTY");						
					for(int i=0; i < len; i++)
					{
						sectionJComboBox.addItem(sectionList[i]);
					}	
				}
			}catch(Exception e)
			{
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				sectionJComboBox.addItem(":");
			}
			sectionJComboBox.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					String[] productList = null;
					try
					{
						String sectionId = (String)sectionJComboBox.getSelectedItem();
						int  sID = Integer.parseInt(sectionId.substring(0, sectionId.indexOf(":")));
						ConnectionToServer connectiontoserver = new ConnectionToServer();
						productList = (String[])connectiontoserver.loadSectionsProduct(sID);						
					}catch(Exception exc)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						sectionsProductTextArea.setText("EMPTY!!!");
						return;
					}
					if (productList != null)
					{	
						int len = productList.length, i=0;
						String str = "";
						for(i=0; i < len;i++)
						{
							str = str+productList[i]+"\n";									
						}
						sectionsProductTextArea.setText(str);					
					}
					else
						JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				}	
			});	
		}
		return sectionJComboBox;
	}
	
	/**
	 * This method initializes cImportanceTextField	
	 */
	private JTextArea getSectionsProductTextArea()
	{
		if (sectionsProductTextArea == null) 
		{
			sectionsProductTextArea = new JTextArea();
			sectionsProductTextArea.setBounds(new Rectangle(150, 290, 230, 80));
			sectionsProductTextArea.setEditable(false);
		}
		return sectionsProductTextArea;
	}
	
	/**
	 * This method initializes acceptButton	
	 */
	private JButton getAcceptButton() 
	{
		if (acceptButton == null) 
		{
			acceptButton = new JButton();
			acceptButton.setBounds(new Rectangle(150, 520, 120, 29));
			acceptButton.setText("Accept");
			acceptButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					try
					{
						String sectionId = (String)sectionJComboBox.getSelectedItem();
						int  sID = Integer.parseInt(sectionId.substring(0, sectionId.indexOf(":")));
						String msgfromserver = customer.addAuthorization(sID);
						if (msgfromserver != null)
						{
							if (msgfromserver.equals("OK"))
								JOptionPane.showMessageDialog(null, "Authorization successfully save");
							else
								JOptionPane.showMessageDialog(null, "Authorization not saved!!!","WARNING", JOptionPane.WARNING_MESSAGE);
						}
						else
							JOptionPane.showMessageDialog(null, "ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
					}catch (ClientNotInitializesException excp)
					{
						JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","CCRM ERROR",JOptionPane.ERROR_MESSAGE);
					}	
				}								
			});						
		}
		return acceptButton;
	}
	
	/**
	 * This method initializes cImportanceTextField	
	 */
	private JTextArea getAllAuthorizationTextArea()
	{
		if (allAuthorizationTextArea == null) 
		{
			allAuthorizationTextArea = new JTextArea();
			allAuthorizationTextArea.setBounds(new Rectangle(150, 400, 230, 80));
			allAuthorizationTextArea.setEditable(false);
			String[] authorizationList = null;
			try
			{
				authorizationList = (String[])customer.loadCustomerAuthorization();						
			}catch(Exception exc)
			{
				JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
				allAuthorizationTextArea.setText("EMPTY!!!");
				return allAuthorizationTextArea;
			}
			if (authorizationList != null)
			{	
				int len = authorizationList.length, i=0;
				String str = "";
				for(i=0; i< len;i++)
				{
					str = str+authorizationList[i]+"\n";					
				}
				allAuthorizationTextArea.setText(str);							
			}
			else
				JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}
		
		return allAuthorizationTextArea;
	}
	
	/**
	 * This method add SaveButton to CustomerForm
	 */
	public void addSaveButton()
	{
		this.add(getSaveButton(), null);
		cImportanceTextField.setText("1");
		cancelButton.setBounds(15, 220, 120, 29);
	}
	
	/**
	 * This method add UpdateButton to CustomerForm
	 */
	public void addUpdateButton()
	{
		this.add(getUpdateButton(),null);
		cIdTextField.setEditable(false);
		cancelButton.setBounds(15, 220, 120, 29);
	}
	
	/**
	 * This method add UpdateButton to CustomerForm
	 */
	public void addAuthorizationButton()
	{
		cIdTextField.setEditable(false);
		cNameTextField.setEditable(false);
		cAgeTextField.setEditable(false);
		cAreaJComboBox.setEnabled(false);
		cPhoneTextField.setEditable(false);
		cImportanceTextField.setEditable(false);
		
		sectionLabel = new JLabel();
		sectionLabel.setBounds(new Rectangle(15, 230, 130, 22));
		sectionLabel.setText("All sections:");
		
		allsectionsproductLabel = new JLabel();
		allsectionsproductLabel.setBounds(new Rectangle(15, 290, 130, 22));
		allsectionsproductLabel.setText("Section products:");
		
		allAuthorizationLabel = new JLabel();
		allAuthorizationLabel.setBounds(new Rectangle(15, 420, 130, 22));
		allAuthorizationLabel.setText("All athorizations:");
		
		this.add(allAuthorizationLabel,null);
		this.add(allsectionsproductLabel,null);
		this.add(sectionLabel,null);
		
		authorizationpane = new JScrollPane(getAllAuthorizationTextArea());
		AdjustmentListener listener = new MyAdjustmentListener();
		authorizationpane.getHorizontalScrollBar().addAdjustmentListener(listener);
		authorizationpane.getVerticalScrollBar().addAdjustmentListener(listener);
		authorizationpane.setBounds(new Rectangle(150, 400, 250, 100));
		this.add(authorizationpane);
		
		sectionScrollPane = new JScrollPane(getSectionsProductTextArea());
		AdjustmentListener listener1 = new MyAdjustmentListener();
		sectionScrollPane.getHorizontalScrollBar().addAdjustmentListener(listener1);
		sectionScrollPane.getVerticalScrollBar().addAdjustmentListener(listener1);
		sectionScrollPane.setBounds(new Rectangle(150, 290, 250, 100));
		this.add(sectionScrollPane);
		
		this.add(getSectionJComboBox(),null);
		this.add(getAcceptButton(), null);	
		this.setSize(450, 600);
	}
}
